package com.cellulam.framework.scheduled;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

/**
 *
 */
@Configuration
public class TaskPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof TaskProcessor) {
            this.registerProcessor((TaskProcessor) bean);
        }
        return bean;
    }

    private void registerProcessor(TaskProcessor processor) {
        Processor config = processor.getClass().getAnnotation(Processor.class);

        if (config == null) {
            return;
        }

        TaskProcessorHolder.holder.registerProcessor(processor, config);
    }

}
